package com.txw.javaCore.server.mq;

import com.google.gson.Gson;
import com.txw.javaCore.api.enums.Constant;
import com.txw.javaCore.model.entity.UserOrder;
import com.txw.javaCore.model.mapper.UserOrderMapper;
import com.txw.javaCore.model.request.DelayJobRequest;
import com.txw.javaCore.model.request.UserLoginRequest;
import com.txw.javaCore.server.service.MailService;
import com.txw.javaCore.server.service.SysLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION: rabbitmq消费者
 * @Author: 涂玄武
 * @DATE: 2021/1/18 17:24
 */
@Component
public class MqListener {

    private static final Logger log = LoggerFactory.getLogger(MqListener.class);

    @Autowired
    private MailService mailService;

    @Autowired
    private UserOrderMapper orderMapper;

    @Autowired
    private SysLogService sysLogService;

    /**
     * rabbitMq死信监听消费消息
     *
     * @param msg
     */
    @RabbitListener(queues = {"${mq.order.basic.queue}"}, containerFactory = "singleListenerContainer")
    public void consumeOrderMsg(@Payload String msg) {
        try {
            log.info("--rabbitmq死信监听消费消息，监听到消息：{}", msg);

            DelayJobRequest request = new Gson().fromJson(msg, DelayJobRequest.class);
            mailService.sendSimpleEmail(request.getSubject(), request.getContent(), request.getTos());
        } catch (Exception e) {
            log.error("rabbitmq死信监听消费信息-发生异常：{}", e);
        }
    }

    /**
     * rabbitMq延迟队列监听消费消息
     *
     * @param msg
     */
    @RabbitListener(queues = {"${mq.delay.queue}"}, containerFactory = "singleListenerContainer")
    public void consumeDelayMsg(@Payload String msg) {
        try {
            log.info("--rabbitmq延迟队列监听消费消息，监听到消息：{}", msg);

            DelayJobRequest request = new Gson().fromJson(msg, DelayJobRequest.class);
            mailService.sendSimpleEmail(request.getSubject(), request.getContent(), request.getTos());
        } catch (Exception e) {
            log.error("rabbitmq延迟队列监听消费信息-发生异常：{}", e);
        }
    }

    /**
     * 监听消费并处理用户登录成功后的消息
     *
     * @param request
     */
    @RabbitListener(queues = "${mq.login.queue.name}", containerFactory = "singleListenerContainer")
    public void consumeLogMsg(@Payload UserLoginRequest request) {
        try {
            log.info("系统日志记录-消费者-监听消费用户登录成功后的消息-内容：{}", request);

            //调用日志记录服务，用于记录用户登录成功后相关登录信息入数据库
            sysLogService.recordLog(request);
        } catch (Exception e) {
            log.error("系统日志记录-消费者-监听消费用户登录成功后的信息-发生异常：{}",request,e.fillInStackTrace());
        }
    }

    /**
     * rabbitMq延迟队列监听消费消息-用户订单
     * @param msg
     */
    @RabbitListener(queues = {"${mq.order.basic.queue.v2}"},containerFactory = "singleListenerContainer")
    public void consumeOrderMsgV2(@Payload String msg){
        try {
            log.info("--rabbitmq延迟队列监听消费消息-用户订单，监听到消息：{}",msg);

            UserOrder order = new Gson().fromJson(msg, UserOrder.class);
            //TODO:  去数据库查询当前订单的信息，判断是否仍然 未支付，如果是，那就意味着 30min 已过，用户仍然没支付;
            //TODO:  如果否(用户已支付)，那就不用管
            order = orderMapper.selectByOrderNo(order.getOrderNo());
            if (Objects.equals(order.getPayStatus(), Constant.OrderPayStatus.UnPay.getCode())){
                orderMapper.updateExpireOrder(order.getOrderNo(),Constant.OrderPayStatus.UnPay.getCode(),Constant.OrderPayStatus.SystemCancel.getCode());
            }
        }catch (Exception e){
            log.error("rabbitmq延迟队列监听消费信息-用户订单-发生异常：{}",e);
        }
    }

}
